package org.jgs1904.apachepoidemo.myPDFBox;


import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class PWGtoPDFConverter {
    public static void main(String[] args) {
        String pwgFilePath = "C:\\Users\\srd\\Desktop\\图纸\\CBHPA22S8M150-A-N24.dwg";
        String outputPdfFilePath = "D:\\output\\CBHPA22S8M150.pdf";

        convertPWGtoPDF(pwgFilePath, outputPdfFilePath);
    }

    private static void convertPWGtoPDF(String pwgFilePath, String outputPdfFilePath) {
        try {
            PDDocument document = new PDDocument();
            PDPage page = new PDPage();
            document.addPage(page);

            PDPageContentStream contentStream = new PDPageContentStream(document, page);

            File pwgFile = new File(pwgFilePath);
            BufferedImage image = ImageIO.read(pwgFile);
            PDImageXObject pdImage = LosslessFactory.createFromImage(document, image);

            contentStream.drawImage(pdImage, 0, 0, image.getWidth(), image.getHeight());

            contentStream.close();

            document.save(outputPdfFilePath);
            document.close();

            System.out.println("PWG to PDF conversion successful!");

        } catch (IOException e) {
            System.out.println("Error converting PWG to PDF: " + e.getMessage());
        }
    }
}
